import { makeAutoObservable } from "mobx";
import myOrderApi from "./myOrderApi";
import { OrderData, OrderState } from "types/type";
import Taro from "@tarojs/taro";

class MyOrderStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });
    }

    orderState: OrderState = "";
    loading: boolean = false;
    page: number = 1;
    pageSize: number = 5;
    total: number = 0;
    list: Array<OrderData> = [];
    hasMore: boolean = true;

    // 获取订单列表
    getNextPage(reset: boolean = false) {
        if (reset) {
            this.page = 1;
            this.hasMore = true;
        } else if (!this.hasMore || this.loading) {
            return;
        } else {
            this.page++;
        }

        this.loading = true;

        myOrderApi.getList({
            orderState: this.orderState,
            pageNum: this.page,
            pageSize: this.pageSize,
            orderCodeMain: ""
        }).then(async res => {

            //将设计图转化成数组的形式
            res.rows.forEach(item => {
                item.childrenList.map(child => {
                    try {
                        child.productDesign = JSON.parse(child.productDesign);
                    } catch (error) {
                    }
                });
            });

            if (reset) {
                this.list = [];
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                this.list = [...res.rows];
            } else {
                this.list = [...this.list, ...res.rows];
            }
            this.total = res.total;
            this.hasMore = (res.rows.length === this.pageSize);
            await new Promise(resolve => setTimeout(resolve, 100));
            this.loading = false;
        });

    }

    //设置状态
    setOrderState(state: OrderState) {
        this.orderState = state;
        this.getNextPage(true);
    }

    // 取消订单
    cancelOrder(orderCodeMain: string) {
        return myOrderApi.cancelOrder(orderCodeMain).then(() => {
            this.getNextPage(true);
        });
    }

    // 确认收货
    ensureOrder(orderCodeMain: string) {
        return myOrderApi.ensureOrder(orderCodeMain).then(() => {
            this.getNextPage(true);
            this.setOrderState("RECEIVED");
        });
    }

    // 删除订单
    deleteOrder(orderCodeMain: string) {
        return myOrderApi.delete(orderCodeMain).then(() => {
            this.getNextPage(true);
        });
    }

    //发起支付
    async payOrder(orderCodeMain: string) {
        const res2 = await myOrderApi.pay(orderCodeMain);
        const payResult = await Taro.requestPayment({
            ...res2.data,
            success: (res: TaroGeneral.CallbackResult) => {
            },
            fail: (res: TaroGeneral.CallbackResult) => {
            },
            complete: (res: TaroGeneral.CallbackResult) => {
                this.getNextPage(true);
            },
        });
    }

}

const myOrderStore = new MyOrderStore();

export default myOrderStore;
